using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class EwmDAL
    {
        public static int Save(EwmInfo info)
        {
            string sql = string.Format("insert into [ewm]([UserID],[ActiveID],[Money],[IsTake]) values({0},{1},{2},{3});select @@identity;", info.UserID, info.ActiveID, info.Money, info.IsTake);
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql));
        }
        public static int Update(int userid,int istake,int id)
        {
            string sql = string.Format("update [ewm] set [UserID]={0},[IsTake]={1} where [ID]={2}", userid, istake, id);
            return CommonDAL.ExecuteNoneQuery(sql) ? id : 0;
        }
        public static EwmInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[ewm]");
            return Simple_Data(dr);
        }
        public static List<EwmInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[ewm]");
            List<EwmInfo> list = new List<EwmInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }

        public static List<EwmInfo> FindPage(int activeid, int startindex, int pagesize, string order, string desc, out int count)
        {
            string filter = " and ActiveID="+activeid;
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[ewm]", out count);
            List<EwmInfo> list = new List<EwmInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static List<EwmInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [ewm]"));
            List<EwmInfo> list = new List<EwmInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static List<EwmInfo> FindByAcitveID(int activeid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [ewm] where activeid=" + activeid));
            List<EwmInfo> list = new List<EwmInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        private static EwmInfo Simple_Data(DataRow dr)
        {
            EwmInfo info = new EwmInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.ActiveID = DataConvert.ToInt(dr["ActiveID"]);
                info.Money = DataConvert.ToFloat(dr["Money"])/100;
                info.IsTake = DataConvert.ToInt(dr["IsTake"]);
                info.IDs = DESEncrypt.Encrypt(info.ID.ToString());
            }
            return info;
        }
    }
}

